package leetcode.dp;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author mazouri
 * @create 2021-09-23 8:57
 */
public class _139_wordBreak {
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = i; j >= 0; j--) {
                dp[i] = dp[j] && set.contains(s.substring(j, i));

                if (dp[i]) break;
            }
        }
        return dp[s.length()];
    }

    public static void main(String[] args) {
        String s = "leetcode";
        ArrayList<String> list = new ArrayList<>();
        list.add("leet");
        list.add("code");
        System.out.println(new _139_wordBreak().wordBreak(s, list));
    }
}
